package com.sansan.qiangji.utils;

import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
public class DateUtil {
    /**
     * 当前日期格式转换
     */
    public static Date nowDate(String dateFormat) {
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        Date date = new Date();
        String dateStr = format.format(date);
        try {
            date = format.parse(dateStr);
        } catch (ParseException e) {
            log.error("【编辑首检记录】日期转换异常 date={}", date);
            e.printStackTrace();
            throw new ServiceException(ResultEnum.DATE_FORMAT_ERROR);
        }
        return date;
    }

    /**
     * 当前日期格式转换
     */
    public static String nowString(String dateFormat) {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat(dateFormat);
        return sf.format(date);
    }
}
